package org.september.pisces.user.permission.controller;

import org.september.core.exception.BusinessException;
import org.september.pisces.user.permission.entity.SystemUser;
import org.september.pisces.user.permission.service.OperationLogService;
import org.september.pisces.user.permission.service.SystemUserService;
import org.september.pisces.user.permission.service.UserOnlineStatusService;
import org.september.pisces.user.permission.utils.UserSessionHelper;
import org.september.simpleweb.auth.DefaultMethod;
import org.september.simpleweb.auth.PublicMethod;
import org.september.simpleweb.controller.BaseController;
import org.september.simpleweb.model.ResponseVo;
import org.september.simpleweb.utils.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;


@Controller
@RequestMapping(value = "")
public class DefaultLoginController extends BaseController{

	@Autowired
	private SystemUserService systemUserService;
	
	@Autowired
	private OperationLogService operationLogService;
	
	@Autowired
	private UserOnlineStatusService userOnlineStatusService;
	
	public static String homePageUrl="";
	
	public static String LoginPageUrl="/defaultLogin";
	
	@PublicMethod
    @RequestMapping("/defaultLogin")
    public ModelAndView defaultLogin(String returnUrl) {
        ModelAndView mv = new ModelAndView();
        if(StringUtils.hasText(returnUrl)) {
        	mv.addObject("returnUrl", returnUrl);
        }else {
        	mv.addObject("returnUrl", homePageUrl);
        }
        return mv;
    }
   
	@PublicMethod
    @ResponseBody
	@RequestMapping(value = "/doLogin")
	public ResponseVo<String> doLogin(String username , String password, String verfiyCode) throws Exception {
    	SystemUser su = systemUserService.login(username, password,false);
    	if(su==null) {
    		throw new BusinessException("用户不存在");
    	}
    	HttpServletRequest req = getRequest();
    	String secch = req.getHeader("sec-ch-ua");
    	String client = "";
    	if(secch==null) {
    		secch = req.getHeader("user-agent");
    	}
    	if(secch!=null) {
    		String[] arr= secch.split(",");
    		client = arr[arr.length-1];
    	}
    	operationLogService.addLog("用户web端登录，IP:"+IpUtils.getIp(getRequest())+","+client);
		return ResponseVo.<String>BUILDER().setCode(ResponseVo.BUSINESS_CODE.SUCCESS).setData("id="+su.getId());
	}
    
	@DefaultMethod
    @ResponseBody
   	@RequestMapping(value = "/logout")
   	public ResponseVo<String> logout() throws Exception {
		operationLogService.addLog("用户web端退出，IP:"+IpUtils.getIp(getRequest()));
		userOnlineStatusService.removeUser(UserSessionHelper.getLoginUid());
    	UserSessionHelper.remove();
    	//TODO 其他清理操作
    	return ResponseVo.<String>BUILDER().setCode(ResponseVo.BUSINESS_CODE.SUCCESS).setData("已退出");
   	}
    
    @PublicMethod
    @RequestMapping("/503")
    public ModelAndView Error503() {
        ModelAndView mv = new ModelAndView();
        return mv;
    }
    
    //TODO 怎么集成
    @PublicMethod
	@RequestMapping("/sso")
	public ModelAndView sso(String accessToken,String fullname,String ssoSecret){
		ModelAndView mv = new ModelAndView();
		mv.addObject("username", accessToken);
		mv.addObject("fullname", fullname);
		mv.addObject("ssoSecret", ssoSecret);
		mv.addObject("pwd", "");
		return mv;
	}
}
